package tcpFirmware.controller;

import tcpFirmware.ResponseBody;
import tcpFirmware.Routes;
//import tcpFirmware.Serial;
import tcpFirmware.model.Serial;
import tcpFirmware.Type;
import tcpFirmware.model.DBHelper;
import tcpFirmware.model.FileHelper;

import java.io.IOException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class MvDetailController {

    @Routes(route = "mvDetail")
    public void get(Socket socket, HashMap<String,String> head, HashMap<String,String> form){

        Connection connection = new DBHelper().getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet set =statement.executeQuery("select * from Movie where name = '"+head.get("mvName")+"'");
            set.next();
            ResponseBody responseBody = new ResponseBody(socket);   //构造一个响应给客户端的数据

            responseBody.setRoute("s").setType(Type.text.type).setHeader("movieName",set.getString("name")).setHeader("img",set.getString("img"));

            responseBody.sendResponse();
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }

    }



    @Routes(route = "getAllMvName")
    public void getAll(Socket socket, HashMap<String,String> head, HashMap<String,String> form){
        Connection connection = new DBHelper().getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet set =statement.executeQuery("select * from Movie");

            HashMap<String,String> map = new HashMap<>();
            int i=0;
            while(set.next()){

                map.put(String.valueOf(i++),set.getString("name"));
            }

            ResponseBody responseBody = new ResponseBody(socket);   //构造一个响应给客户端的数据

            responseBody.setRoute("s").setType(Type.text.type).setData(map);

            responseBody.sendResponse();
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }


    @Routes(route = "getAllMovie")
    public void getAllMovie(Socket socket, HashMap<String,String> head, HashMap<String,String> form){
        String name=form.get("name");
        Connection connection= new DBHelper().getConnection();
        try {
            Statement statement=connection.createStatement();
            ResultSet rs=statement.executeQuery("select * from Movie where name='"+name+"'");
            rs.next();
            String marketTime=rs.getString("MarketTime");
            String detail = rs.getString("detail").replace("\r","").replace("\n","").replaceAll("\\s*", "");
            String price = rs.getString("price");

            ResponseBody responseBody = new ResponseBody(socket);
            responseBody.setType(Type.json.type).setRoute("getAllMovie");
            responseBody.setHeader("marketTime",marketTime).setHeader("price",price).setHeader("detail",detail);

            ResultSet rs2=statement.executeQuery("select * from Center where name='"+name+"'");
            ArrayList<HashMap<String,String>> data = new ArrayList<>();
            while (rs2.next()){
                HashMap<String,String> map = new HashMap<>();
                map.put("session",rs2.getString("session"));
                map.put("center",rs2.getString("centerNum"));
                map.put("howLong",rs2.getString("howLong"));
                map.put("whenStart",rs2.getString("whenStart"));
                data.add(map);
            }

            responseBody.sendResponse(Serial.objectToBytes(data),"notAFile");
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }
}

